package com.zf.shuashua.api;

import android.content.Intent;

import com.zf.shuashua.ui.repair.RepairActivity;
import com.zf.shuashua.utils.ToastUtil;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.ui.mine.LoginActivity;

public abstract class AbsCallback<T> extends ACallback<T> {


    @Override
    public void onSuccess(T data) {
        if (data instanceof BaseData) {
            BaseData data1 = (BaseData) data;
            if (data1.getCode() == 402 || data1.getCode() == 406) {
                ToastUtil.showToast(ShuaApplication.getContext(), "请重新登录");
                Store.getInstance().logoutSiglent(ShuaApplication.getContext());
                Intent intent = new Intent(ShuaApplication.getContext(), LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ShuaApplication.getContext().startActivity(intent);
            } else if (data1.getCode() == 401) {
                Store.getInstance().logoutSiglent(ShuaApplication.getContext());
            } else if (data1.getCode() == 407) {
                Intent intent = new Intent(ShuaApplication.getContext(), RepairActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                ShuaApplication.getContext().startActivity(intent);
            } else if (data1.getCode() == 421) {
                ToastUtil.showToast(ShuaApplication.getContext(), data1.getMessage());
            } else if (data1.getCode() == 422) {
                ToastUtil.showToast(ShuaApplication.getContext(), data1.getMessage());
            }else if (data1.getData() != null) {
                onRealSuccess((T) data1);
            } else {
//                ToastUtil.showToast(ShuaApplication.getContext(), "error code is  " + data1.getCode());
            }
        }
    }


    public abstract void onRealSuccess(T data);

    @Override
    public void onFail(int errCode, String errMsg) {

    }
}